<template>
  <div class="login-background">
<!--    <div class="logo"></div>-->
    <div class="box-bg"></div>
    <div class="box-bg3">
      <img style="width: 45%" src="/images/bg3.svg">
    </div>
    <div class="login">
      <div style="width: 100%;margin: 0 auto;padding: 20px;box-sizing: border-box">
        <h1>{{ appConfig.name }}</h1>
        <div class="form-item x-row">
<!--          <div class="x-ml-5"><el-icon color="#c5c5c5" size="22"><User /></el-icon></div>-->
<!--          <input v-model="username" type="text" placeholder="请输入账号">-->
          <el-input text placeholder="请输入账号" size="large" v-model="username" clearable
                    autocomplete="off">
            <template #prefix>
              <el-icon class="el-input__icon">
                <User/>
              </el-icon>
            </template>
          </el-input>
        </div>

        <div class="form-item x-row">
          <el-input :type="showPassword ? 'text' : 'password'" size="large" placeholder="请输入密码" v-model="password" autocomplete="off">
            <template #prefix>
              <el-icon class="el-input__icon">
                <Unlock/>
              </el-icon>
            </template>
<!--            <template #suffix>-->
<!--              <i class="iconfont el-input__icon login-content-password" :class="showPassword ? 'icon-yincangmima' : 'icon-xianshimima'" @click="showPassword = !showPassword"></i>-->
<!--            </template>-->
          </el-input>
<!--          <div class="x-ml-5"><el-icon color="#c5c5c5" size="22"><Lock /></el-icon></div>-->
<!--          <input v-model="password" type="password" placeholder="请输入密码" @keyup.enter.native="login">-->
        </div>

        <div class="form-item x-row">
          <el-input text size="large" maxlength="4" placeholder="请输入验证码" v-model="verifyCode" clearable
                    autocomplete="off">
            <template #prefix>
              <el-icon class="el-input__icon">
                <Position/>
              </el-icon>
            </template>
          </el-input>
          <div class="x-pl-20">
            <img :src="codeImg" @click="getCode">
          </div>
        </div>

        <div class="btn">
          <el-button  :loading="loading" @click="login">登录</el-button>
        </div>

        <div class="notice">
          * 温馨提示：建议使用谷歌、Microsoft Edge，版本 80 及以上浏览器，360浏览器请使用极速模式
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import md5 from 'js-md5'
import {GlobalConfig, useStore} from 'core';

const {appConfig} = GlobalConfig();
import MenuList from '@/config/menu';

const {menu, user} = useStore();

import {onMounted} from "vue";
import Base64 from '@/util/base64.js';
let username = ref("");
let verifyCode = ref("");
let password = ref("");
let showPassword = ref(false);
let loading = ref(false);

const router = useRouter();

let codeImg = ref(null)
let captchaKey = ''
const getCode = async () => {
  captchaKey = Helper.genRandomStr(32)
  codeImg.value = "http://127.0.0.1:9999" + appConfig.http.prefix +  "/code/image?randomStr=" + captchaKey;
}
onMounted(() => {
  Cache.remove('token')
  getCode()
})

let captcha = ref(null)
const login = async () => {

  if (Helper.empty(username.value)) {
    Msg.error("请输入账号");
    return;
  }

  loading.value = true;

  let resp = await Http.post("/oauth2/token", {username: username.value,
    password: Helper.encryption(password.value, import.meta.env.VITE_APP_PWD_ENC_KEY),
    randomStr: captchaKey, code: verifyCode.value,
    grant_type: "password", scope: "server"})
      .addHeaders("Skiptoken", true)
      .addHeaders("Content-type", "application/x-www-form-urlencoded")
      .addHeaders("Authorization", "Basic " + Base64.encoder(import.meta.env.VITE_APP_OAUTH2_PASSWORD_CLIENT))
      .setInterceptor(null, (response) => {
        response = window.JSON.parse(response);
        let code = response.code;
        let res = {};
        if(! Helper.empty(code)) {
          res = {code: 500, msg: response.msg, data: response.data};
        } else {
          res = {code: 0, msg: "调用成功", data: response};
        }
        return res;
      })

  loading.value = false;

  if (resp.code == 200) {
    Msg.success('登录成功');
    let token = resp.data.access_token;
    Cache.setToken(token);
    await menu.init();
    // let userInfo = await user.getUserInfo();
    router.push({path: appConfig.home});
  } else {
    getCode()
  }
}

</script>


<style scoped lang="less">
.login-background {
  width: 100%;
  height: 100%;
  position: relative;
  left: 0;
  top: 0;
  z-index: 90;
  background-color: #f0f5fd;
}

.logo {
  width: 295px;
  height: 34px;
  position: absolute;
  top: 30px;
  left: 30px;
  background: url("/images/logo.png") no-repeat center center;
  z-index: 9;
}

.box-bg {
  width: 100%;
  height: 100%;
  position: absolute;
  //top: 10%;
  //left: -15%;
  background: url("/images/bg.png") no-repeat center center;
}

.box-bg3 {
  position: absolute;
  bottom: 30%;
  left: 20%;
}

.login {
  width: 400px;
  border-radius: 4px;
  overflow: hidden;
  position: absolute;
  left: 100%;
  top: 50%;
  margin: -200px 0 0 -600px;
  z-index: 999;
  background-color: #fff;

  h1 {
    text-align: center;
    font-size: 24px;
  }

  input {
    width: 100%;
    height: 20px;
    line-height: 36px;
    background-color: transparent;
    border: none;
    border-radius: 2px;
    font-size: 14px;
    text-indent: 5px;
    outline: none;
  }

  input::placeholder {
    color: #999999;
  }

  .form-item {
    width: 100%;
    margin-top: 20px;
  }

  .btn {
    width: 100%;
    margin-top: 40px;
  }

  .btn button {
    width: 100%;
    display: inline-block;
    height: 44px;
    line-height: 44px;
    padding: 0 18px;
    background-color: @primary;
    color: #fff;
    white-space: nowrap;
    text-align: center;
    font-size: 18px;
    border: none;
    border-radius: 2px;
    cursor: pointer;
  }

  .notice {
    margin-top: 20px;
    color: #a8abb2;
    font-size: 12px;
    text-align: center;
  }
}
</style>
